package bm15;

/**
 * @author 兴趣使然黄小黄
 * @version 1.0
 * @date 2023/7/23 1:16
 * BM15.删除有序链表中的重复元素-I
 * https://www.nowcoder.com/practice/c087914fae584da886a0091e877f2c79?tpId=295&tqId=664&ru=/exam/oj&qru=/ta/format-top101/question-ranking&sourceUrl=%2Fexam%2Foj
 */
public class Solution {

    // 思路：快慢指针
    public ListNode deleteDuplicates (ListNode head) {
        // 边界判断
        if (head == null || head.next == null) {
            return head;
        }
        // 快慢指针思路删除
        ListNode pre = head;
        ListNode cur = head.next;
        while(cur != null) {
            while (cur != null && cur.val == pre.val) {
                cur = cur.next;
            }
            pre.next = cur;
            pre = cur;
            if (cur != null) {
                cur = cur.next;
            }
        }
        return head;
    }

    private static class ListNode {
        int val;
        ListNode next = null;
        public ListNode(int val) {
            this.val = val;
        }
    }
}
